<template>
	<view class="p-bot50" :style="themeColor">
		<status-bar title="销售人员管理"></status-bar>
		<u-sticky :customNavHeight="totalHeight" v-if="pageData.length">
			<view class="navInfo">
				<text class="black size32">累计 {{pageItem.count}} 位</text>
				<view class=" d-flex a-center">
					<text class="iconfont icon-zengjia4 color size34"></text>
					<text class="color size30 p-left20" @click="show = true">添加成员</text>
				</view>
			</view>
		</u-sticky>




		<Members :pageData="pageData" v-if="pageData.length"></Members>

		<view class="noMemers" v-else @click="show = true">
			<view class="iconfont icon-a-ICON icon-tianjia color_dd size100"></view>
			<text class="color_cc size30 m-top40">暂未配置人员请点击添加</text>
		</view>

		<u-popup :show="show" @close="show = false" :duration="150" round="10" closeable>
			<view class="Popup">
				<view class="Title">我的会员</view>
				<view class="Warper">
					<view class="WarperList" v-for="item in People" :key="item.v_id" @click.stop="isAdd(item)">
						<view class="flex-1 d-flex a-center">
							<image :src="$imgUrls(item.v_headimg)" mode="aspectFill" class="headimg"></image>
							<view class="peopleInfo">
								<view class="d-flex a-center ellipsis">
									<text class="black size30 v_name">{{item.v_name}}</text>
									<text class="color_99 h6">{{item.v_tel}}</text>
								</view>
								<text class="color_99 h6 m-top10">{{item.v_addtime}}</text>
							</view>
						</view>
						<view class="Selects" :class="{'Active':isSelected(item.v_id)}">
							<text class="iconfont icon-xuanzhong white size28"></text>
						</view>
					</view>
				</view>

				<view class="Popconfig">
					<view class="ConfigBtn flexac" :class="{'bgcolor':vids.length}" @click="configAdd">确定</view>
				</view>
			</view>
		</u-popup>
	</view>
</template>


<script>
	import {
		mapState,
		mapGetters
	} from 'vuex'
	import Members from './components/Members'
	export default {
		components: {
			Members
		},
		data() {
			return {
				limit: 10,
				page: 1,
				loadMore: true,
				pageItem: '',
				pageData: [],
				show: false,
				People: [],
				vids: [],
			}
		},
		onLoad() {
			this.getDirectSalesMember();
		},
		methods: {
			getDirectSalesMember() {1
				this.$http.post({
					url: '/osmxcxApi/getDirectSalesMember',
					data: {
						limit: this.limit,
						page: this.page
					}
				}).then(res => {
					if (res.code != 100) return this.$Toast(res.msg)
					const result = res.data || []
					this.pageItem = res;
					this.page == 1 ? this.pageData = result : this.pageData = [...this.pageData, ...result]
					this.distribution_people();
					if (result.length < this.limit) this.loadMore = false
				})
			},
			distribution_people() {
				this.$http.post({
					url: '/zzj_singleSaleApi/distribution_people',
					data: {
						directSales: 1
					}
				}).then(res => {
					if (res.errcode != 100) return this.$Toast(res.msg)
					const result = res.data || []
					if (result.length) this.People = result[0]
				})
			},

			configAdd() {
				if (!this.vids.length) return
				this.$showLoading('')
				this.$http.post({
					url: '/osmxcxApi/addDirectSalesMember',
					data: {
						vids: this.vids.join(',')
					}
				}).then(res => {
					this.$hideLoading()
					this.vids = [];
					this.show = false;
					this.$Toast(res.msg);
					this.page = 1;
					this.getDirectSalesMember();
				})
			},

			isAdd(Item) {
				const index = this.vids.indexOf(Item.v_id);
				if (index === -1) {
					this.vids.push(Item.v_id); // 如果未选中，则添加
				} else {
					this.vids.splice(index, 1); // 如果已选中，则移除
				}
			},
			isSelected(v_id) {
				return this.vids.includes(v_id);
			}
		},
		onReachBottom() {
			if (!this.loadMore) return;
			this.page++;
			this.getDirectSalesMember();
		},
		computed: {
			...mapState(['totalHeight']),
			...mapGetters(['themeColor'])
		}
	}
</script>

<style lang="scss">
	page {
		background-color: $page;
	}
</style>
<style lang="scss" scoped>

	.navInfo {
		width: 100%;
		background-color: #941717;
		padding: 20rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;
		border-radius: 0 0 10rpx 10rpx;
		border-top: 1rpx solid #f2f2f2;
	}

	.noMemers {
		width: 100%;
		display: flex;
		flex-direction: column;
		align-items: center;
		padding-top: 140rpx;
	}

	.Popup {
		width: 100%;
		background-color: $page;
		border-radius: 20rpx 20rpx 0 0;
		overflow: hidden;

		.Title {
			width: 100%;
			height: 88rpx;
			background-color: #ffffff;
			display: flex;
			justify-content: center;
			align-items: center;
			color: #000;
			font-size: 36rpx;
		}

		.Warper {
			width: 690rpx;
			height: 900rpx;
			overflow-y: scroll;
			padding-bottom: 30rpx;
			margin: 0 auto;

			.WarperList {
				width: 100%;
				padding: 20rpx;
				background-color: #ffffff;
				margin-top: 20rpx;
				border-radius: 10rpx;
				display: flex;
				align-items: center;

				.headimg {
					width: 80rpx;
					height: 80rpx;
					border-radius: 6rpx;
					margin-right: 20rpx;
				}

				.peopleInfo {
					display: flex;
					flex-direction: column;

					.v_name {
						max-width: 300rpx;
						margin-right: 16rpx;
						overflow: hidden;
						text-overflow: ellipsis;
						display: -webkit-box;
						-webkit-line-clamp: 1;
						-webkit-box-orient: vertical;
					}
				}

				.Selects {
					width: 36rpx;
					height: 36rpx;
					border: 1px solid #ccc;
					border-radius: 6rpx;
					overflow: hidden;
					display: flex;
					justify-content: center;
					align-items: center;

				}

				.Active {
					border: 1px solid var(--color);
					background-color: var(--color);
				}
			}

		}

		.Popconfig {
			width: 100%;
			height: 120rpx;
			background-color: #ffffff;
			display: flex;
			justify-content: center;
			align-items: center;


			.ConfigBtn {
				width: 690rpx;
				height: 80rpx;
				font-size: 36rpx;
				color: #fff;
				border-radius: 10rpx;
				background-color: #ddd;
			}
		}
	}
</style>